package cn.com.digitalnet.ad.util;

import java.io.File;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/***********
 * 根据包名来获取此包下所有的类名及其实例 以及 和表有关的转换操作
 * @param packName
 * @return
 */
@Component
public class ClassUtil {
	
	@Autowired private LogUtil lu;
	
	//用于判断 忽略基类的几个属性 字段
	public static final String IGNORE_STR="id,addTime,addUser,updateTime,updateUser,isenabled,isdeleted,";
	public static List<String> getClassNamesInPackage(String packageName){
		List<String> classNames = Lists.newArrayList();
		String packageDirName = packageName.replace(".", "/");
		Enumeration<URL> dirs = null;
		try {
			
			dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
			//迭代此 Enumeration
			while(dirs.hasMoreElements()){
				URL url = dirs.nextElement();
				File file = new File(url.getFile());
				//把此目录下的所有文件列出
				String[] classes = file.list();
				//循环此数组，并把.class去掉
				for(String className : classes){
					className = className.substring(0,className.length()-6);
					if(!(className.equalsIgnoreCase("log")||className.equalsIgnoreCase("config"))){
						String fullName = packageName + "." + className;
						Class clazz = Class.forName(fullName);
						if(!Modifier.isAbstract(clazz.getModifiers())){
							classNames.add(className);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return classNames;
	}
	

	//有表得到所有的字段 、类型 信息
	public void Table2Prop(String tableName,MybatisUtil bt,Map<String,String> javaParam,List<Map<String, String>> fields) throws Exception{
		SqlSession session = bt.getSqlSession();
		Connection conn = session.getConnection();
		DatabaseMetaData dm = conn.getMetaData();
		ResultSet rs =  dm.getColumns(null, "root", tableName, null);
		Map<String,String> xmlProps = Maps.newHashMap();
		while(rs.next()){
			
			String prop= lu.fieldToProperty(rs.getString("COLUMN_NAME"));
			if(!IGNORE_STR.contains(prop)){
				
				javaParam.put(prop, getJavaType(rs.getString("TYPE_NAME")));
				xmlProps.put(lu.propertyToField(prop), prop);
			}
		}
		fields.add(xmlProps);
	}
	
	//转换成java类型
	public String getJavaType(String fieldType) {
		// TODO Auto-generated method stub
		
		if(fieldType.equalsIgnoreCase("long")){
			return "long";
		}else if(fieldType.equalsIgnoreCase("bigint")){
			return "long";
		}else if(fieldType.equalsIgnoreCase("decimal")){
			return "BigDecimal";
		}
		else if(fieldType.equalsIgnoreCase("dateTime")){
			return "date";
		}else{
			return "string";
		}
	}
	
	public String getTbalePrefix(MybatisUtil bt) throws Exception{
		SqlSession session = bt.getSqlSession();
		Connection conn = session.getConnection();
		DatabaseMetaData dm = conn.getMetaData();
		ResultSet rs = dm.getTables(null, "root", null, new String[]{"TABLE"});
		String tableName="";
		String prefix="";
		if(rs.next()){
			tableName = rs.getString("TABLE_NAME");
		}
		prefix=StringUtils.substringBefore(tableName, "_");
		
		if(prefix.equalsIgnoreCase("basic")){
			prefix="tbl";
		}
		return prefix;
	}
	
}
